#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System.IO;
using System.IO.Compression;
using System.Text;

namespace AbstractClass.Extensions
{
    /// <summary>
    /// Provides a set of extension methods for <see cref="byte"/> array.
    /// </summary>
    public static class ByteArrayExtensions
    {
        /// <summary>
        ///   Gets the string representations of a byte array.
        /// </summary>
        /// <param name = "byteArray">The byte array to convert to string.</param>
        /// <returns>
        /// A <see cref="string"/> object equivalent to <paramref name="byteArray"/>.
        /// </returns>
        public static string GetString(this byte[] byteArray)
        {
            return Encoding.Unicode.GetString(byteArray);
        }

        /// <summary>
        /// Compresses the specified byte array.
        /// </summary>
        /// <param name="data">The byte array to compress.</param>
        /// <returns>
        /// A compressed byte array.
        /// </returns>
        public static byte[] Compress(this byte[] data)
        {
            using (var output = new MemoryStream())
            {
                var gzip = new GZipStream(output, CompressionMode.Compress, true);
                gzip.Write(data, 0, data.Length);
                return output.ToArray();
            }
        }

        /// <summary>
        /// Decompresses the specified byte array.
        /// </summary>
        /// <param name="data">The byte array to compress.</param>
        /// <returns>
        /// Decompressed byte array.
        /// </returns>
        public static byte[] Decompress(this byte[] data)
        {
            using (var input = new MemoryStream())
            {
                input.Write(data, 0, data.Length);
                input.Position = 0;

                var gzip = new GZipStream(input, CompressionMode.Decompress, true);
                using (var output = new MemoryStream())
                {
                    var buff = new byte[64];
                    int read = gzip.Read(buff, 0, buff.Length);
                    while (read > 0)
                    {
                        output.Write(buff, 0, read);
                        read = gzip.Read(buff, 0, buff.Length);
                    }

                    return output.ToArray();
                }
            }
        }
    }
}